/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} A
 * @param {TreeNode} B
 * @return {boolean}
 */

function match(root1, root2) {
  if (!root2) return true;  // root2遍历结束
  if (!root1) return false;

  if (root1.val != root2.val) return false;
  return match(root1.left, root2.left) && match(root1.right, root2.right);
}
var isSubStructure = function (A, B) {
  if (!B || !A) return false; // 空树不属于任何树的子树，任何树也不属于空树的子树
  if (match(A, B)) return true;

  if (isSubStructure(A.left, B) || isSubStructure(A.right, B)) return true;
  return false;
};